using System;
using System.ComponentModel;

#if SILVERLIGHT

namespace System.ComponentModel
{
    // Summary:
    //     Provides data for a cancelable event.
    public class CancelEventArgs : EventArgs
    {
        // Summary:
        //     Initializes a new instance of the System.ComponentModel.CancelEventArgs class
        //     with the System.ComponentModel.CancelEventArgs.Cancel property set to false.
        public CancelEventArgs()
        {
        }
        //
        // Summary:
        //     Initializes a new instance of the System.ComponentModel.CancelEventArgs class
        //     with the System.ComponentModel.CancelEventArgs.Cancel property set to the
        //     given value.
        //
        // Parameters:
        //   cancel:
        //     true to cancel the event; otherwise, false.
        public CancelEventArgs(bool cancel)
        {
            Cancel = cancel;
        }

        // Summary:
        //     Gets or sets a value indicating whether the event should be canceled.
        //
        // Returns:
        //     true if the event should be canceled; otherwise, false.
        public bool Cancel { get; set; }
    }

}

#endif

namespace SBPweb.Forge.Common.Exceptions
{

	/// <summary>
	/// Class of ExceptionHandler
	/// </summary>
	public class ExceptionHandler
	{
		/// <summary>
		/// Class of ExceptionEventArgs
		/// </summary>
        public class ExceptionEventArgs : CancelEventArgs
        {

            private System.Exception exception;
            private string category;

			/// <summary>
			/// Constructor of ExceptionEventArgs
			/// </summary>
			/// <param name="exception">exception</param>
			/// <param name="category">category</param>
            public ExceptionEventArgs(System.Exception exception, string category)
            {
                this.exception = exception;
                this.category = category;
            }

			/// <summary>
			/// The exception
			/// </summary>
            public System.Exception Exception
            {
                get
                {
                    return exception;
                }
            }

			/// <summary>
			/// Category of the exception
			/// </summary>
            public string Category
            {
                get
                {
                    return category;
                }
            }

        }

		/// <summary>
		/// Handler of the ExceptionHandler
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
        public delegate void ExceptionHandlerHandler(object sender, ExceptionEventArgs e);

		/// <summary>
		/// ExceptionHandler of exception
		/// </summary>
        public static event ExceptionHandlerHandler OnException;

#if !SILVERLIGHT
		/// <summary>
		/// ExceptionHandler of SOAP exception
		/// </summary>
        public static event ExceptionHandlerHandler OnSoapException;
		/// <summary>
		/// ExceptionHandler of SQL exception
		/// </summary>
        public static event ExceptionHandlerHandler OnSqlException;
#endif

		/// <summary>
		/// Handles the exception (with no category)
		/// </summary>
		/// <param name="ex">exception</param>
		public static void HandleException(System.Exception ex)
		{
			HandleException(ex, string.Empty);
		}

		/// <summary>
		/// Handles the exception
		/// </summary>
		/// <param name="ex">exception</param>
		/// <param name="category">category</param>
		public static void HandleException(System.Exception ex, string category)
		{
            if (OnException != null)
            {
                ExceptionEventArgs eea = new ExceptionEventArgs(ex, category);
#if !SILVERLIGHT
                OnException(System.Web.HttpContext.Current, eea);
#else
                OnException(null, eea);
#endif
                if (eea.Cancel)
                {
                    return;
                }
            }
			throw ex;
		}

#if !SILVERLIGHT
		/// <summary>
		/// Handles a SOAP exception
		/// </summary>
		/// <param name="ex">exception</param>
		public static void HandleSoapException(System.Web.Services.Protocols.SoapException ex)
		{
			HandleSoapException(ex, string.Empty);
		}

		/// <summary>
		/// Handles a SOAP exception
		/// </summary>
		/// <param name="ex">exception</param>
		/// <param name="category">category</param>
		public static void HandleSoapException(System.Web.Services.Protocols.SoapException ex, string category)
		{
            if (OnSoapException != null)
            {
                ExceptionEventArgs eea = new ExceptionEventArgs(ex, category);
                OnSoapException(System.Web.HttpContext.Current, eea);
                if (eea.Cancel)
                {
                    return;
                }
            }
			throw ex;
		}

		/// <summary>
		/// Handles a SQL exception
		/// </summary>
		/// <param name="ex">exception</param>
		public static void HandleSqlException(System.Data.SqlClient.SqlException ex)
		{
			HandleSqlException(ex, string.Empty);
		}

		/// <summary>
		/// Handles a SQL exception
		/// </summary>
		/// <param name="ex">exception</param>
		/// <param name="category">category</param>
		public static void HandleSqlException(System.Data.SqlClient.SqlException ex, string category)
		{
            if (OnSqlException != null)
            {
                ExceptionEventArgs eea = new ExceptionEventArgs(ex, category);
                OnSqlException(System.Web.HttpContext.Current, eea);
                if (eea.Cancel)
                {
                    return;
                }
            }
			throw ex;
		}
#endif

	}
}
